'''
Created on Aug 13, 2012

@author: petrina
'''

from indicator import Indicator
import logging

class Link_indicator(Indicator):
    '''
    this class is abstract and represents one indicator for one Link.
    
    The Link_indicator receives aggregated FLEET-data when it is initialized.
    The indicator may calculate its values for each time interval and
    export the data as csv or png.
    '''


    def __init__(self, link):
        '''
        @param link: Link-object
        '''
        
        Indicator.__init__(self)
        self._link = link
        
        if self._link is not None:
            self._intervals = link.records.intervals
        
        logging.debug("Indicator: created Indicator")
            

        
    #----------------------private methods of the class------------------------

    def _create_title(self, id = None, direction = None):
        '''
        uses link information in order to set the indicator name in plots and exported files
        '''
        
        self._title = self._name
        self._exp_title = self._mr_name
        
        info = id.__str__().strip()
        self._title += ' for ' + info
        self._exp_title += '_' + info
            
        info = direction.__str__().strip()
        self._title += ' (' + info + ')'
        self._exp_title += '_' + info.lower()
            
    
    def _plot_data(self, plotobject, attribute):
        '''
        plots the indicator and data of the given attribute (e.g. travel time or speed).
        
        The indicator values will be red and connected whereas the data is light grey.
        
        @param plotobject: new plot object (@see output.plot)
        @param attribute: attribute of Agg_record, which shall be plotted, as string (e.g. speed or traveltime)
        '''
        
        self._create_plot(plotobject)
        
        # plot data --> bad datastructure: cannot draw time rows 
        for i in self._link.records:
            if len(self._link.records[i]) > 0:               
                for rec in self._link.records[i]:
                    plotobject.plot(i, getattr(rec, attribute), col = '#999999', mark='+', markersize = 2)
                       
        # plot indicator
        plotobject.plot(range(len(self._values)), self._values)
        
        # export plot
        plotobject.save_plot2png(self._exp_title, 'png')         
